<template>
  <el-container class="home_container">
    <el-header height="100px">
      <div class="left">
        <span>电商管理系统</span>
      </div>
      <div class="right">
        <el-button type="danger" @click="logout">退出</el-button>
      </div>
    </el-header>
    <el-container>
      <!--侧边栏  -->
      <el-aside :width="isCollapse ? '65px' : '200px'">
        <!-- 侧边栏菜单区域 -->
        <el-menu
          active-text-color="#409BFF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="currentPath"
        >
          <div class="toggleCollapse" @click="toggleCollapse">|||</div>
          <!-- 一级菜单 -->
          <el-submenu
            v-for="menu in menuList"
            :key="menu.id"
            :index="menu.id + ''"
          >
            <template slot="title">
              <i :class="iconsObj[menu.id]" class="icon"></i>
              <span>{{ menu.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + menuChild.path"
              v-for="menuChild in menu.children"
              :key="menuChild.id"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ menuChild.authName }}</span>
              </template></el-menu-item
            >
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script type="text/ecmascript-6">
import { reqMenus } from "../../api";
export default {
  data() {
    return {
      menuList: [],
      // 让一级菜单显示不同的图标
      iconsObj: {
        125: "iconfont icon-user",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-changyongtubiao-fuben-64",
        145: "iconfont icon-icon",
      },
      // 是否收缩菜单列表
      isCollapse: false,
    };
  },
  mounted() {
    this.getMenuList();
  },
  computed: {
    currentPath() {
      return this.$route.path;
    },
  },
  methods: {
    // 退出登录
    logout() {
      this.$confirm("此操作将退出页面, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        sessionStorage.removeItem("token_key");
        this.$router.replace("/login");
        this.$message({
          type: "success",
          message: "退出成功!",
        });
      });
    },
    //获取所有菜单
    async getMenuList() {
      const result = await reqMenus();
      if (result.meta.status !== 200) this.$message.error(result.meta.msg);
      else {
        this.menuList = result.data;
      }
    },
    // 水平收缩列表
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
  },
};
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;

  .el-header {
    display: flex;
    line-height: 100px;
    justify-content: space-between;
    background-color: #28b7f5;
    .left span {
      font-size: 30px;
    }
  }
  .el-aside {
    background-color: #ccc;
    .toggleCollapse {
      width: 100%;
      height: 30px;
      line-height: 30px;
      letter-spacing: 0.2rem;
      text-align: center;
      cursor: pointer;

      background-color: rgb(173, 169, 169);
    }
    .el-menu {
      border-right: 0;
      .icon {
        margin-right: 10px;
      }
    }
  }
  .el-main {
    background-color: #e9edf1;
  }
}
</style>